<?php

App::uses('AppController', 'Controller');

/**
 * CoreBlocksModules Controller
 *
 * @property CoreBlocksModule $CoreBlocksModule
 */
class CoreBlocksModulesController extends AppController {

    /**
     * index method
     *
     * @return void
     */
    public function index() {
        $this->CoreBlocksModule->recursive = 0;
        $this->set('coreBlocksModules', $this->paginate());
    }

    /**
     * view method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function view($id = null) {
        if (!$this->CoreBlocksModule->exists($id)) {
            throw new NotFoundException(__('Invalid core blocks module'));
        }
        $options = array('conditions' => array('CoreBlocksModule.' . $this->CoreBlocksModule->primaryKey => $id));
        $this->set('coreBlocksModule', $this->CoreBlocksModule->find('first', $options));
    }

    /**
     * add method
     *
     * @return void
     */
    public function add() {
        if ($this->request->is('post')) {
            $this->CoreBlocksModule->create();
            if ($this->CoreBlocksModule->save($this->request->data)) {
                $this->Session->setFlash(__('The core blocks module has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The core blocks module could not be saved. Please, try again.'));
            }
        }
        $coreModules = $this->CoreBlocksModule->CoreModules->find('list', array('fields' => array('CoreModules.id', 'CoreModules.como_name')));
        $coreBlocks = $this->CoreBlocksModule->CoreBlocks->find('list', array('fields' => array('CoreBlocks.id', 'CoreBlocks.cobl_name')));
        $this->set(compact('coreModules', 'coreBlocks'));
    }

    /**
     * edit method
     *
     * @throws NotFoundException
     * @param string $id
     * @return void
     */
    public function edit($id = null) {
        if (!$this->CoreBlocksModule->exists($id)) {
            throw new NotFoundException(__('Invalid core blocks module'));
        }
        if ($this->request->is('post') || $this->request->is('put')) {
            if ($this->CoreBlocksModule->save($this->request->data)) {
                $this->Session->setFlash(__('The core blocks module has been saved'));
                $this->redirect(array('action' => 'index'));
            } else {
                $this->Session->setFlash(__('The core blocks module could not be saved. Please, try again.'));
            }
        } else {
            $options = array('conditions' => array('CoreBlocksModule.' . $this->CoreBlocksModule->primaryKey => $id));
            $this->request->data = $this->CoreBlocksModule->find('first', $options);
        }
        $coreModules = $this->CoreBlocksModule->CoreModules->find('list', array('fields' => array('CoreModules.id', 'CoreModules.como_name')));
        $coreBlocks = $this->CoreBlocksModule->CoreBlocks->find('list', array('fields' => array('CoreBlocks.id', 'CoreBlocks.cobl_name')));
        $this->set(compact('coreModules', 'coreBlocks'));
    }

    /**
     * delete method
     *
     * @throws NotFoundException
     * @throws MethodNotAllowedException
     * @param string $id
     * @return void
     */
    public function delete($id = null) {
        $this->CoreBlocksModule->id = $id;
        if (!$this->CoreBlocksModule->exists()) {
            throw new NotFoundException(__('Invalid core blocks module'));
        }
        $this->request->onlyAllow('post', 'delete');
        if ($this->CoreBlocksModule->delete()) {
            $this->Session->setFlash(__('Core blocks module deleted'));
            $this->redirect(array('action' => 'index'));
        }
        $this->Session->setFlash(__('Core blocks module was not deleted'));
        $this->redirect(array('action' => 'index'));
    }
    
    public function getModulesIdFromBlock($blockId){
        $options = array('conditions' => array('CoreBlocksModule.core_blocks_id'=> $blockId));
        $data = $this->CoreBlocksModule->find('all', $options);
        $modules = array();
        foreach ($data as $value) {
            $modules[] = intval($value["CoreBlocksModule"]["core_modules_id"]);
        }
        return $modules;
    }

}
